﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using OfficeOpenXml;

namespace Core
{
    public class Team : List<Person>
    {
        public Team()
        {
            var excelPackage = Core.OpenExcelFileWithWaiting(Configuration.ReportFilePath);

            var worksheet = excelPackage.Workbook.Worksheets["Config"];
            int currentRow = 3;
            while (worksheet.Cells[currentRow, 1].Value != null)
            {
                var person = new Person();
                person.Name = worksheet.Cells[currentRow, 1].Value.ToString();
                person.Login = worksheet.Cells[currentRow, 2].Value.ToString().ToLower();
                person.Email = worksheet.Cells[currentRow, 3].Value.ToString();
                person.Role = worksheet.Cells[currentRow, 4].Value.ToString().ToLower();
                Add(person);
                currentRow++;
            }

            excelPackage.Dispose();
        }

        public bool ContainsName(string name)
        {
            return this.Where(p => p.Name == name).FirstOrDefault() != null;
        }

        public Person GetNextPersonByRole(string role, string previousName)
        {
            if (previousName == null)
                return GetFirstPersonByRole(role);

            var list = this.Where(p => p.Role == role).ToList();
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Name == previousName)
                {
                    if (i + 1 < list.Count)
                        return list[i + 1];
                    else
                        return list[0];
                }
            }
            return null;
        }

        public Person GetFirstPersonByRole(string role)
        {
            return this.Where(p => p.Role == role.ToLower()).FirstOrDefault();
        }

        public Person GetFirstPersonByLogin(string login)
        {
            return this.Where(p => p.Login == login.ToLower()).FirstOrDefault();
        }

        public Person TeamLead
        {
            get { return this.GetFirstPersonByRole("team lead"); }
        }

        public List<Person> Developers
        {
            get { return this.Where(p => p.Role == "developer").ToList(); }
        }

        //public List<Person> Coaches
        //{
        //    get { return this.Where(p => p.Role == "coach").ToList(); }
        //}
    }
}
